#include "CFootball.h"
#include <string>

CFootball::CFootball(void)
	: m_Ball()
{
	m_Teams[0] = CFootballTeam("Rot");
	m_Teams[1] = CFootballTeam("Schwarz", &m_Teams[0]);
}

CFootball::CFootball(std::string _NameTeam1, std::string _NameTeam2)
	: m_Ball()
{
	m_Teams[0] = CFootballTeam(_NameTeam1);
	m_Teams[1] = CFootballTeam(_NameTeam2, &m_Teams[0]);
}

CFootball::~CFootball(void)
{
}

std::string CFootball::PlayGame()
{
	int PlayingTeam = GetRandom(0, 1);
	CFootballPlayer* pPlayer = NULL;
	CFootballKeeper* pKeeper = NULL;
	CFootballOutfieldPlayer* pFielder = NULL;
	
	m_Ball.SetBall(m_Teams[PlayingTeam].GiveBallRandomPlayer());
	pPlayer = m_Ball.GetBall();

	do{
		if(pPlayer->Pass()) {

			if(PlayingTeam == 0) {

				if(m_Teams[PlayingTeam++].Intercept()) {	
					while(!m_Ball.SetBall(m_Teams[PlayingTeam].GiveBallRandomPlayer()));
				} else {	
					while(!m_Ball.SetBall(m_Teams[PlayingTeam--].GiveBallNextPlayer(pPlayer)));
				}
			} else {

				if(m_Teams[PlayingTeam--].Intercept()) {	
					while(!m_Ball.SetBall(m_Teams[PlayingTeam].GiveBallRandomPlayer()));
				} else {	
					while(!m_Ball.SetBall(m_Teams[PlayingTeam++].GiveBallNextPlayer(pPlayer)));
				}
			}
		} else { //Goalshot
			pFielder = (CFootballOutfieldPlayer *) pPlayer;
			if(pFielder->Shot()) {
				pFielder = (CFootballOutfieldPlayer *) pPlayer;
				if(PlayingTeam == 0) {
					pKeeper = m_Teams[PlayingTeam++].GetKeeper();
				} else {
					pKeeper = m_Teams[PlayingTeam--].GetKeeper();
				}

				if(pKeeper->Keep()) {
					while(!m_Ball.SetBall(pPlayer));
				} else {
					m_Teams[PlayingTeam].IncrementScore();
					while(!m_Ball.SetBall(m_Teams[PlayingTeam].GiveBallRandomPlayer()));
				}
			} else {

				if(PlayingTeam == 0) {
					pPlayer = m_Teams[PlayingTeam++].GetKeeper();
				} else {
					pPlayer = m_Teams[PlayingTeam--].GetKeeper();
				}
				while(!m_Ball.SetBall(pPlayer));
			}
		}
	}while(m_Teams[0].GetScore() == 10 || m_Teams[1].GetScore() == 10);
	//std::string::t
	
	return (m_Teams[0].GetName() + " " + (char)m_Teams[0].GetScore() + " : " + (char)m_Teams[1].GetScore() + " " + m_Teams[1].GetName());
}
